package com.EquationSolver;

import com.EquationSolver.SquareEquation;
import com.EquationSolver.GraphPanel;

import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Component;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.BoxLayout;


public class MainFrame extends JFrame implements DocumentListener {

	private static final long serialVersionUID = -3234451831663453318L;
	private JPanel contentPane;
	private JTextField textFieldA;
	private JTextField textFieldB;
	private JTextField textFieldC;
	
	private GraphPanel graphPanel;
	
	private SquareEquation equation;
	private JPanel panel;
	private JPanel graphTitlePanel;
	private JLabel equationLabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame.setDefaultLookAndFeelDecorated(true);
					
					MainFrame frame = new MainFrame("Square equation graph");
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	double tryParseDouble(String s)
	{
		try{
		return  Double.parseDouble(s);
		}catch(Exception ex)
		{
			return Double.NaN;
		}
	}

	void UpdateEquation()
	{
		double a = tryParseDouble(textFieldA.getText());
		double b = tryParseDouble(textFieldB.getText());
		double c = tryParseDouble(textFieldC.getText());
		
		equation = new SquareEquation(a,b,c);
		equationLabel.setText(equation.description());
		graphPanel.setFormula(equation);
		
		graphPanel.repaint();
	}
	
    
   public void insertUpdate(DocumentEvent ev) {
	   UpdateEquation(); 
   }
    
   public void removeUpdate(DocumentEvent ev) {
	   UpdateEquation(); 
   }
    
   public void changedUpdate(DocumentEvent ev) {
	   UpdateEquation(); 
   }
    
	
	/**
	 * Create the frame.
	 */
	public MainFrame(String title) {
		setTitle(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 527, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		
		setContentPane(contentPane);
		
		panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		graphTitlePanel = new JPanel();
		graphTitlePanel.setMaximumSize(new Dimension(32767, 24));
		panel.add(graphTitlePanel);
		
		JLabel graphLabel = new JLabel("Graph:");
		graphTitlePanel.add(graphLabel);
		graphLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		equationLabel = new JLabel("");
		graphTitlePanel.add(equationLabel);
		
		graphPanel = new GraphPanel();
		panel.add(graphPanel);
		graphPanel.setBackground(Color.WHITE);
		graphPanel.setLayout(null);
		
		JPanel inputPanel = new JPanel();
		contentPane.add(inputPanel, BorderLayout.NORTH);
		inputPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblA = new JLabel("A:");
		inputPanel.add(lblA);
		
		textFieldA = new JTextField();
		textFieldA.setText("0.01");
		textFieldA.getDocument().addDocumentListener(this);
		inputPanel.add(textFieldA);
		textFieldA.setColumns(10);
		
		JLabel lblB = new JLabel("B:");
		inputPanel.add(lblB);
		
		textFieldB = new JTextField();
		textFieldB.setText("0");
		textFieldB.getDocument().addDocumentListener(this);
		inputPanel.add(textFieldB);
		textFieldB.setColumns(10);
		
		JLabel lblC = new JLabel("C:");
		inputPanel.add(lblC);
		
		textFieldC = new JTextField();
		textFieldC.setText("0");
		textFieldC.getDocument().addDocumentListener(this);
		inputPanel.add(textFieldC);
		textFieldC.setColumns(10);
		
		UpdateEquation();
	}

}

